package com.ciko.code.fragment;


import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.ciko.code.adapter.PublicityListAdater;
import com.ciko.code.base.BaseFragment;
import com.ciko.code.bean.Module;
import com.ciko.code.bean.ModuleContent;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.Req.QryModuleContentListReq;
import com.ciko.code.http.Req.QryModuleListReq;
import com.ciko.code.http.Req.SleBannerListReq;
import com.ciko.code.views.pullrefresh.PullToRefreshListView;
import com.mywork.R;
import com.mywork.activity.WebActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 12154 on 2015/10/29.
 */
public class PublicityFragment extends BaseFragment implements AdapterView.OnItemClickListener{

    private View v_lanmu1;
    private View v_lanmu2;
    private View v_lanmu3;
    private View v_lanmu4;

    private TextView tv_itemPublic_1;
    private TextView tv_itemPublic_2;
    private TextView tv_itemPublic_3;
    private TextView tv_itemPublic_4;

    private PullToRefreshListView plv_publicity;
    private ListView lv_publicity;

    private PublicityListAdater publicityListAdater;

    private List<ModuleContent> moduleContentList = new ArrayList<>();

    private View handerView;
    private List<Module> moduleList;

    @Override
    protected void initTopBar() {
        unableNav();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.fragment_publicity);
    }

    @Override
    protected void initView() {
        plv_publicity = findViewById(R.id.lv_publicity);
        lv_publicity = plv_publicity.getRefreshableView();
        handerView = LayoutInflater.from(getContext()).inflate(R.layout.layout_publicity_header, null);
        lv_publicity.addHeaderView(handerView);

        v_lanmu1 = handerView.findViewById(R.id.v_lanmu1);
        v_lanmu2 = handerView.findViewById(R.id.v_lanmu2);
        v_lanmu3 = handerView.findViewById(R.id.v_lanmu3);
        v_lanmu4 = handerView.findViewById(R.id.v_lanmu4);

        tv_itemPublic_1 = (TextView) handerView.findViewById(R.id.tv_itemPublic_1);
        tv_itemPublic_2 = (TextView) handerView.findViewById(R.id.tv_itemPublic_2);
        tv_itemPublic_3 = (TextView) handerView.findViewById(R.id.tv_itemPublic_3);
        tv_itemPublic_4 = (TextView) handerView.findViewById(R.id.tv_itemPublic_4);
    }

    @Override
    protected void initAdapter() {
        publicityListAdater = new PublicityListAdater(getContext(), moduleContentList, R.layout.item_list_publicity);
        lv_publicity.setAdapter(publicityListAdater);
    }

    @Override
    protected void initDates() {
        lv_publicity.setOnItemClickListener(this);
    }

    @Override
    protected void initListener() {
        tv_itemPublic_1.setOnClickListener(this);
        tv_itemPublic_2.setOnClickListener(this);
        tv_itemPublic_3.setOnClickListener(this);
        tv_itemPublic_4.setOnClickListener(this);
    }

    @Override
    protected void initHttpRequest() {
        HttpClient.getInstance(getContext()).post(new SleBannerListReq(), this);
        HttpClient.getInstance(getContext()).post(new QryModuleListReq(), this);

    }

    @Override
    public void onRequestStart(String tag) {
        super.onRequestStart(tag);
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {
        super.onResponseSuccess(response, tag);

        switch (tag) {
            case "QryModuleListReq":
                moduleList = (List<Module>) response.getReturnObject();
                tv_itemPublic_1.setText(moduleList.get(0).getModuleName());
                tv_itemPublic_2.setText(moduleList.get(1).getModuleName());
                tv_itemPublic_3.setText(moduleList.get(2).getModuleName());
                tv_itemPublic_4.setText(moduleList.get(3).getModuleName());
                HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(0).getModuleId(), 0, 10), this);
                break;
            case "QryModuleContentListReq":
                List<ModuleContent> moduleContentsResult = (List<ModuleContent>) response.getReturnObject();
                moduleContentList.clear();
                moduleContentList.addAll(moduleContentsResult);
                publicityListAdater.notifyDataSetChanged();
                break;
        }


    }

    @Override
    public void onResponseFail(String tag,int resultCode) {
        super.onResponseFail(tag,resultCode);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if(v==tv_itemPublic_1){
            HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(0).getModuleId(), 0, 10), this);

            v_lanmu1.setBackgroundColor(getResources().getColor(R.color.nav_bg_root));
            v_lanmu2.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu3.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu4.setBackgroundColor(getResources().getColor(R.color.transparent));
        } else if(v==tv_itemPublic_2){
            HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(1).getModuleId(), 0, 10), this);
            v_lanmu1.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu2.setBackgroundColor(getResources().getColor(R.color.nav_bg_root));
            v_lanmu3.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu4.setBackgroundColor(getResources().getColor(R.color.transparent));
        } else if(v==tv_itemPublic_3){
            HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(2).getModuleId(), 0, 10), this);
            v_lanmu1.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu2.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu3.setBackgroundColor(getResources().getColor(R.color.nav_bg_root));
            v_lanmu4.setBackgroundColor(getResources().getColor(R.color.transparent));
        } else if(v==tv_itemPublic_4){
            HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(3).getModuleId(), 0, 10), this);
            v_lanmu1.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu2.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu3.setBackgroundColor(getResources().getColor(R.color.transparent));
            v_lanmu4.setBackgroundColor(getResources().getColor(R.color.nav_bg_root));
        }

//        switch (v.getId()) {
//            case R.id.tv_itemPublic_1:
//                HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(0).getModuleId(), 0, 10), this);
//
//                v_lanmu1.setBackgroundColor(getResources().getColor(R.color.nav_bg_root));
//                v_lanmu2.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu3.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu4.setBackgroundColor(getResources().getColor(R.color.transparent));
//                break;
//            case R.id.tv_itemPublic_2:
//                HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(1).getModuleId(), 0, 10), this);
//                v_lanmu1.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu2.setBackgroundColor(getResources().getColor(R.color.nav_bg_root));
//                v_lanmu3.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu4.setBackgroundColor(getResources().getColor(R.color.transparent));
//                break;
//            case R.id.tv_itemPublic_3:
//                HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(2).getModuleId(), 0, 10), this);
//                v_lanmu1.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu2.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu3.setBackgroundColor(getResources().getColor(R.color.nav_bg_root));
//                v_lanmu4.setBackgroundColor(getResources().getColor(R.color.transparent));
//                break;
//            case R.id.tv_itemPublic_4:
//                HttpClient.getInstance(getContext()).post(new QryModuleContentListReq(moduleList.get(3).getModuleId(), 0, 10), this);
//                v_lanmu1.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu2.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu3.setBackgroundColor(getResources().getColor(R.color.transparent));
//                v_lanmu4.setBackgroundColor(getResources().getColor(R.color.nav_bg_root));
//                break;
//        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if(id != -1){
            ModuleContent moduleContent = (ModuleContent) parent.getAdapter().getItem(position);

            if(moduleContent.getHasHtml().equals("y")) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("URL", moduleContent.getHtmlUrl());
                getContext().startActivity(intent);
            }
        }

    }
}
